﻿Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.SqlTypes
Imports System.Runtime.InteropServices
Imports Microsoft.SqlServer.Server

Partial Public Class UserDefinedFunctions
    <Microsoft.SqlServer.Server.SqlFunction(FillRowMethodName:="MFBreakFileNameWithPath_FillRow", IsDeterministic:=True, IsPrecise:=False, TableDefinition:="FilePath nvarchar(128), FileName nvarchar(128)")> _
    Shared Function MFBreakFileNameWithPath(ByVal FileNameWithPath As SqlString) As IEnumerable
        Dim _sFileNameWithPath(0) As String

        Try
            _sFileNameWithPath(0) = FileNameWithPath.ToString.Trim

            Return _sFileNameWithPath
        Catch sqlex As SqlException
            Throw New Exception("FileName: " & _sFileNameWithPath(0), sqlex)
        Catch ex As Exception
            Throw New Exception("FileName: " & _sFileNameWithPath(0), ex)
        End Try
    End Function

    Shared Sub MFBreakFileNameWithPath_FillRow(ByVal obj As Object, <Out()> ByRef FilePath As SqlString, <Out()> ByRef FileName As SqlString)
        Dim _sFileNameWithPath As String = CType(obj, String)

        Try
            FilePath = System.IO.Path.GetDirectoryName(_sFileNameWithPath).Trim & "\"
            FileName = System.IO.Path.GetFileName(_sFileNameWithPath)
        Catch sqlex As SqlException
            Throw New Exception("FileName: " & _sFileNameWithPath, sqlex)
        Catch ex As Exception
            Throw New Exception("FileName: " & _sFileNameWithPath, ex)
        End Try
    End Sub
End Class
